package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.CompanyUser;
import com.leoman.service.CompanyAuthService;
import com.leoman.service.CompanyUserService;
import com.leoman.service.PersonalAuthService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 企业用户管理
 * Created by 涂奕恒 on 2016/10/21.
 */
@Controller
@RequestMapping(value = "backend/companyUser")
public class CompanyUserController extends CommonController {

    @Autowired
    private CompanyUserService companyUserService;

    @Autowired
    private PersonalAuthService personalAuthService;

    @Autowired
    private CompanyAuthService companyAuthService;

    @RequestMapping("index")
    public String index() {
        return "backend/企业用户列表";
    }

    @RequestMapping("indexToCheck")
    public String indexToCheck() {
        return "backend/企业用户认证列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String mobile,
                     String nickName,
                     Integer type,
                     Integer status,
                     Integer sortType,
                     Integer isCheck,
                     Integer checkStatus,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<CompanyUser> page = companyUserService.pageByParams(mobile, nickName, type, status, sortType, isCheck, checkStatus, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer companyUserId, Integer flag) {
        model.addAttribute("flag", flag);

        if (null != companyUserId) {
            CompanyUser companyUser = companyUserService.getById(companyUserId);
            model.addAttribute("companyUser", companyUser);
            if (null != companyUser) {
                if (companyUser.getType().equals(Constant.COMPANY_USER_TYPE_PERSONAL)) {
                    // 个人广告主
                    model.addAttribute("personalAuth", personalAuthService.findOneByCompanyUserId(companyUserId));
                } else if (companyUser.getType().equals(Constant.COMPANY_USER_TYPE_COMPANY)) {
                    // 企业广告主
                    model.addAttribute("companyAuth", companyAuthService.findOneByCompanyUserId(companyUserId));
                }
            }
        }

        return "backend/企业用户详情";
    }

    /**
     * 启用 or 禁用广告主
     *
     * @param companyUserId 用户ID
     * @param status        状态
     * @return
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Integer changeStatus(Integer companyUserId, Integer status) {
        try {
            Integer[] companyUserIds = new Integer[1];
            companyUserIds[0] = companyUserId;
            return companyUserService.changeCompanyUserStatus(companyUserIds, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 审核广告主
     *
     * @param companyUserId 用户ID
     * @param status        状态
     * @return
     */
    @RequestMapping("/changeCheckStatus")
    @ResponseBody
    public Integer changeCheckStatus(Integer companyUserId, Integer status, String failReasonContent) {
        try {
            Integer[] companyUserIds = new Integer[1];
            companyUserIds[0] = companyUserId;
            return companyUserService.changeCompanyUserCheckStatus(companyUserIds, status, failReasonContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量修改企业用户状态
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchUpdate")
    @ResponseBody
    public Integer batchUpdate(String ids, Integer status) {
        try {
            Integer[] arrayId = JsonUtil.json2Obj(ids, Integer[].class);
            return companyUserService.changeCompanyUserStatus(arrayId, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量审核通过 or 审核失败广告主
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchCheck")
    @ResponseBody
    public Integer batchCheck(String ids, Integer status, String failReasonContent) {
        try {
            Integer[] arrayId = JsonUtil.json2Obj(ids, Integer[].class);
            return companyUserService.changeCompanyUserCheckStatus(arrayId, status, failReasonContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 广告主账户余额编辑
     *
     * @param companyUserId 用户ID
     * @param money         余额
     * @return
     */
    @RequestMapping("/changeMoney")
    @ResponseBody
    public Integer changeMoney(Integer companyUserId, Double money) {
        try {
            return companyUserService.changeCompanyUserMoney(companyUserId, money);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
